package com.lvandroid.play;

import android.app.Application;
import android.content.Context;
import android.view.LayoutInflater;

import com.lvandroid.play.util.LogUtil;
import com.lvandroid.player.BuildConfig;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;

import java.util.HashMap;

public class PlayerApplication {

    private static PlayerApplication sINSTANCE;
    private static byte[] sLOCK = new byte[0];

    private Application mApp;

    private PlayerApplication() {}

    public static PlayerApplication getInstance() {
        if(null == sINSTANCE) {
            synchronized (sLOCK) {
                if(null == sINSTANCE) {
                    sINSTANCE = new PlayerApplication();
                }
            }
        }

        return sINSTANCE;
    }

    void init(Application app) throws IllegalArgumentException {
        if(null == app || !(app instanceof Application)) {
            LogUtil.throwException("PlayerApplication", "IllegalArgumentException when sdk init.");
            throw new IllegalArgumentException("We need a Application object param.");
        }

        mApp = app;

        initX5WebView();
    }

    /**
     * 腾讯x5 webview提前初始化
     */
    private void initX5WebView() {
        HashMap map = new HashMap();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);
        QbSdk.initX5Environment(mApp, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                LogUtil.i("PlayerApplication", "onCoreInitFinished.");
            }

            @Override
            public void onViewInitFinished(boolean b) {
                LogUtil.i("PlayerApplication", "onViewInitFinished : " + b);
            }
        });
    }

    public static Context getGlobalContext() {
        return getInstance().mApp;
    }

    public static LayoutInflater getLayoutInflater() {
        return LayoutInflater.from(getInstance().getGlobalContext());
    }

}
